package com.starsky.project.controller;


import com.starsky.common.util.DateUtils;
import com.starsky.common.util.easypoi.ExcelUtils;
import com.starsky.project.domain.dto.BookInfoDto;
import com.starsky.project.domain.entity.EvaluationModel;
import com.starsky.project.domain.vo.BookInfoVo;
import com.starsky.project.service.ExcelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Api("excel基本操作")
@RestController
public class ExcelController {

    @Autowired
    private ExcelService excelService;

    /**
     * 导入普通excel,从第一行开始读取
     */
    @ApiOperation("导入普通excel,从第几行开始读取")
    @PostMapping(value = "/importBookInfo")
    public List<BookInfoDto> importBookInfo(MultipartFile file) {
        return excelService.importExcel(file);
    }

    /**
     * 导入合并单元格excel
     */
    @ApiOperation("导入合并单元格excel")
    @PostMapping(value = "/importMergeCellExcel")
    public List<EvaluationModel> importMergeCellExcel(MultipartFile file) {
        return excelService.importMergeCellExcel(file);
    }


    /**
     * 导出普通excel
     */
    @ApiOperation("导出普通excel")
    @PostMapping(value = "/exportBookInfo")
    public void exportBookInfo(HttpServletResponse response, BookInfoVo param) {
        List<BookInfoDto> list = excelService.exportBookInfo(param);
        String fileName = "图书信息-" + DateUtils.format(new Date(), DateUtils.YYYYMMDDHHMMSS);
        ExcelUtils.exportExcel(response, fileName, list, BookInfoDto.class);
    }

    /**
     * 多sheet导出
     */
    @ApiOperation("多sheet导出")
    @PostMapping(value = "/exportMutiSheet")
    public void exportMutiSheet(HttpServletResponse response, BookInfoVo param) {
        List<Map<String, Object>> list = excelService.exportMutiSheet(param);
        String fileName = "图书信息-" + DateUtils.format(new Date(), DateUtils.YYYYMMDDHHMMSS);
        ExcelUtils.exportExcel(response, list, fileName);
    }

    /**
     * 动态生成excel模板
     */
    @ApiOperation("动态生成excel模板")
    @PostMapping(value = "/exportExcelTemplate")
    public void exportMutiSheet(HttpServletResponse response) {
        excelService.exportExcelTemplate(response);
    }

    /**
     * 根据模版动态填充excel数据
     */
    @ApiOperation("根据模版动态填充excel数据")
    @PostMapping(value = "/exportDynamicDataExcel")
    public void exportDynamicDataExcel(HttpServletResponse response) {
        excelService.exportDynamicDataExcel(response);
    }

    /**
     * 生成excel多级级联
     */
    @ApiOperation("生成excel多级级联")
    @PostMapping(value = "/exportCascadeExcel")
    public void exportCascadeExcel(HttpServletResponse response) {
        excelService.exportCascadeExcel(response);
    }

}
